package com.liujun.spring4.demo.aop.aop.cglibproxy;

import net.sf.cglib.proxy.Enhancer;
import net.sf.cglib.proxy.MethodInterceptor;
import net.sf.cglib.proxy.MethodProxy;

import java.lang.reflect.Method;

/**
 * 动态代理类的实现
 *
 * @author liujun
 * @version 0.0.1
 * @date 2018/09/19
 */
public class CglibProxy implements MethodInterceptor {

  private Enhancer enhancer = new Enhancer();

  public Object getProxy(Class targerClass) {
    enhancer.setSuperclass(targerClass);
    enhancer.setCallback(this);
    return enhancer.create();
  }

  @Override
  public Object intercept(Object o, Method method, Object[] objects, MethodProxy methodProxy)
      throws Throwable {

    PerformanceMonitor.begin(o.getClass().getName() + "." + method.getName());
    Object result = methodProxy.invokeSuper(o, objects);
    PerformanceMonitor.end();

    return result;
  }
}
